#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void handler(int sig)
{
    while (1)
    {
        pid_t id = waitpid(-1, NULL, WNOHANG);
        if (id > 0)
        {
            std::cout << "关闭" << getpid() << std::endl;
        }
        else if (id == 0)
        {
            std::cout << "暂时关闭" << std::endl;
            break;
        }
        else
        {
            std::cout << "全部关闭" << std::endl;
            break;
        }
        /* code */
    }
}

int main()
{
    signal(SIGCHLD, handler);
    //signal(SIGCHLD, SIG_IGN);//另一种方法

    //当10个甚至更多个子进程一起来时
    for (int i = 0; i < 10; i++)
    {
        if (fork() == 0)
        {
            sleep(3);
            exit(1);
        }
    }

    while (1)
    {
        sleep(3);
        /* code */
    }

    return 0;
}